/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import controllers.entity.DeTai;
import controllers.process.CanBoProcess;
import controllers.process.DeTaiProcess;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import models.Constant;
import views.JPanel.TTCanBo;

/**
 *
 * @author HoaDuyen
 */
public class DlgThemDeTai extends java.awt.Dialog {

    private CanBoProcess canBoProcess;
    private DeTaiProcess deTaiProcess;
    private int macanbo;
    private DeTai detai;
    private TTCanBo parent;
    public DlgThemDeTai(JFrame parent, boolean modal,int macanbo,DeTai detai ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.detai=detai;
        this.macanbo=macanbo;
        this.setResizable(false);
        canBoProcess=new CanBoProcess();
        deTaiProcess=new DeTaiProcess();
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        initComponents();
        ok.setText("Thêm");
        if (detai != null) { // Dang o che do sua
            tendetai.setText(detai.getTenDeTai());
            cap.setText(detai.getCap());
            kinhphi.setText(""+detai.getKinhPhi());
            thoigianbatdau.setText(detai.getStart());
            thoigianketthuc.setText(detai.getFinish());
            chutridetai.setText(detai.getChuDeTai());
            tinhtrang.setText(detai.getTinhTrang());
            ketqua.setText(detai.getKetQua());
            txtGiaDinh.setText("Sửa thông tin đề tài");
            ok.setText("Lưu");
        }
    }
     public boolean kiemtrasonguyen(String s){
        int n=s.length();
        for (int i=0;i<n;i++)
            if (s.charAt(i)<'0'||s.charAt(i)>'9') return false;
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel18 = new javax.swing.JPanel();
        jLabel34 = new javax.swing.JLabel();
        jLabel35 = new javax.swing.JLabel();
        jLabel36 = new javax.swing.JLabel();
        tendetai = new javax.swing.JTextField();
        kinhphi = new javax.swing.JTextField();
        cap = new javax.swing.JTextField();
        jLabel39 = new javax.swing.JLabel();
        chutridetai = new javax.swing.JTextField();
        jPanel19 = new javax.swing.JPanel();
        jLabel37 = new javax.swing.JLabel();
        jLabel38 = new javax.swing.JLabel();
        jLabel40 = new javax.swing.JLabel();
        jLabel41 = new javax.swing.JLabel();
        thoigianbatdau = new javax.swing.JTextField();
        thoigianketthuc = new javax.swing.JTextField();
        tinhtrang = new javax.swing.JTextField();
        ketqua = new javax.swing.JTextField();
        txtGiaDinh = new javax.swing.JLabel();
        cancelcon = new javax.swing.JButton();
        ok = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel34.setText("Tên đề tài");

        jLabel35.setText("Cấp");

        jLabel36.setText("Kinh phí");

        kinhphi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kinhphiActionPerformed(evt);
            }
        });

        jLabel39.setText("Chủ trì đề tài");

        javax.swing.GroupLayout jPanel18Layout = new javax.swing.GroupLayout(jPanel18);
        jPanel18.setLayout(jPanel18Layout);
        jPanel18Layout.setHorizontalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel18Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel18Layout.createSequentialGroup()
                        .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel36)
                            .addComponent(jLabel39))
                        .addGap(33, 33, 33)
                        .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(kinhphi, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(chutridetai)))
                    .addGroup(jPanel18Layout.createSequentialGroup()
                        .addComponent(jLabel34)
                        .addGap(47, 47, 47)
                        .addComponent(tendetai, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel18Layout.createSequentialGroup()
                        .addComponent(jLabel35)
                        .addGap(76, 76, 76)
                        .addComponent(cap, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel18Layout.setVerticalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel18Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel34)
                    .addComponent(tendetai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel35)
                    .addComponent(cap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36)
                    .addComponent(kinhphi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel39)
                    .addComponent(chutridetai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jLabel37.setText("Năm bắt đầu");

        jLabel38.setText("Năm kết thúc");

        jLabel40.setText("Tình trạng");

        jLabel41.setText("Kết quả");

        javax.swing.GroupLayout jPanel19Layout = new javax.swing.GroupLayout(jPanel19);
        jPanel19.setLayout(jPanel19Layout);
        jPanel19Layout.setHorizontalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel19Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel19Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel37)
                            .addComponent(jLabel38)
                            .addComponent(jLabel40))
                        .addGap(54, 54, 54))
                    .addGroup(jPanel19Layout.createSequentialGroup()
                        .addComponent(jLabel41)
                        .addGap(78, 78, 78)))
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ketqua, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tinhtrang, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(thoigianketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(thoigianbatdau, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel19Layout.setVerticalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel19Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel37)
                    .addComponent(thoigianbatdau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(thoigianketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel38))
                .addGap(23, 23, 23)
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel40)
                    .addComponent(tinhtrang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel41)
                    .addComponent(ketqua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        txtGiaDinh.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtGiaDinh.setText("Thêm Đề Tài");

        cancelcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        cancelcon.setText("Hủy");
        cancelcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelconActionPerformed(evt);
            }
        });

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        ok.setText("Thêm");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(233, 233, 233)
                        .addComponent(ok))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jPanel18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelcon))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(txtGiaDinh)
                .addGap(239, 239, 239))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtGiaDinh)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jPanel18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelcon)
                    .addComponent(ok))
                .addGap(44, 44, 44))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        if (tendetai.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tendetai.selectAll();
            tendetai.requestFocus();
            return;
        }
         if (cap.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            cap.selectAll();
            cap.requestFocus();
            return;
        }
          if (chutridetai.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            chutridetai.selectAll();
            chutridetai.requestFocus();
            return;
        }
           if (tinhtrang.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tinhtrang.selectAll();
            tinhtrang.requestFocus();
            return;
        }
        if (ketqua.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            ketqua.selectAll();
            ketqua.requestFocus();
            return;
        }
        if (tendetai.getText().equals("")||
           (cap.getText().equals(""))||
           (thoigianbatdau.getText().equals(""))||
            (thoigianketthuc.getText().equals(""))||
             (tinhtrang.getText().equals(""))||
               (ketqua.getText().equals(""))||
                (chutridetai.getText().equals(""))
                 ){
            
            JOptionPane.showMessageDialog(this, "Bạn phải nhập đầy đủ thông tin !");
            return;
        }
        if (!kiemtrasonguyen(thoigianbatdau.getText())){
             JOptionPane.showMessageDialog(this, "Thời gian bắt đầu sai!");
             thoigianbatdau.selectAll();
             thoigianbatdau.requestFocus();
             return;
        }
        if (!kiemtrasonguyen(thoigianketthuc.getText())){
             JOptionPane.showMessageDialog(this, "Thời gian kết thúc sai !");
             thoigianketthuc.selectAll();
             thoigianketthuc.requestFocus();
             return;
        }
        Integer start=Integer.parseInt(thoigianbatdau.getText());
        Integer finish=Integer.parseInt(thoigianketthuc.getText());
        if (start<1990){
            JOptionPane.showMessageDialog(this, "Thời gian bắt đầu sai !");
             thoigianbatdau.selectAll();
             thoigianbatdau.requestFocus();
            return;
        }
        if (finish>2020){
            JOptionPane.showMessageDialog(this, "Thời gian kết thúc sai !");
             thoigianketthuc.selectAll();
             thoigianketthuc.requestFocus();
             return;
        }
        if (start>finish){
             JOptionPane.showMessageDialog(this, "Thời gian kết thúc sai !");
             thoigianketthuc.selectAll();
             thoigianketthuc.requestFocus();
             return;
        }
        int dem = DeTaiProcess.getCountDetai()+1;
        String tmadetai = "" + dem;
        
        String ttendetai=tendetai.getText();
        String tcap=cap.getText();
        float tkinhphi;
        try {
            tkinhphi= Float.parseFloat(kinhphi.getText());
        } catch (Exception e){
            JOptionPane.showMessageDialog(this,"Nhập số thực !");
            kinhphi.selectAll();
            kinhphi.requestFocus();
            return;
        }
        if (tkinhphi<0){
             JOptionPane.showMessageDialog(this,"kinh phí không đúng!");
            kinhphi.selectAll();
            kinhphi.requestFocus();
            return;
        }
        int startt,finishh;
        try {
            startt= Integer.parseInt(thoigianbatdau.getText());
        } catch (Exception e){
            JOptionPane.showMessageDialog(this,"Nhập số nguyên  !");
            thoigianbatdau.selectAll();
            thoigianbatdau.requestFocus();
            return;
        }
        try {
            startt= Integer.parseInt(thoigianketthuc.getText());
        } catch (Exception e){
            JOptionPane.showMessageDialog(this,"Nhập số nguyên  !");
            thoigianketthuc.selectAll();
            thoigianketthuc.requestFocus();
            return;
        }
        String tstart=thoigianbatdau.getText();
        String tfinish=thoigianketthuc.getText();
        String ttinhtrang=tinhtrang.getText();
        String tketqua=ketqua.getText();
        String tchudetai=chutridetai.getText();
       
        DeTai ct=new DeTai(macanbo, tmadetai, ttendetai, tcap, tkinhphi, tstart, tfinish, tchudetai, ttinhtrang, tketqua);
        if (detai == null) { // Them moi sach
            if (DeTaiProcess.insertDeTai(ct)) {
                JOptionPane.showMessageDialog(this, "Thêm thành công!");
                parent.loadListDeTai();
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Failed!");
            }
        } else {
            //sua sach
            ct.setMaDeTai(detai.getMaDeTai());
            if (DeTaiProcess.updateDeTai(ct)) {
                JOptionPane.showMessageDialog(this, "Sửa thành công!");
                parent.loadListDeTai();
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Failed!");
            }
        }
        
    }//GEN-LAST:event_okActionPerformed

    private void cancelconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelconActionPerformed
        dispose();
    }//GEN-LAST:event_cancelconActionPerformed

    private void kinhphiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kinhphiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_kinhphiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelcon;
    private javax.swing.JTextField cap;
    private javax.swing.JTextField chutridetai;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel37;
    private javax.swing.JLabel jLabel38;
    private javax.swing.JLabel jLabel39;
    private javax.swing.JLabel jLabel40;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JTextField ketqua;
    private javax.swing.JTextField kinhphi;
    private javax.swing.JButton ok;
    private javax.swing.JTextField tendetai;
    private javax.swing.JTextField thoigianbatdau;
    private javax.swing.JTextField thoigianketthuc;
    private javax.swing.JTextField tinhtrang;
    private javax.swing.JLabel txtGiaDinh;
    // End of variables declaration//GEN-END:variables
}
